package com.qinker.oa.dao;

import com.qinker.oa.model.CustomerContactMessage;
import com.qinker.oa.model.CustomerInfo;
import com.qinker.oa.model.vo.CustomerInfoVo;
import com.qinker.oa.provider.CustomerProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CustomerInfoDao {

    @Select("select ac.id,customer_name as customerName,customer_number as customerNumber, customer_area as customerArea," +
            "customer_wechat as customerWechat,customer_contact as customerContact,create_time as createTime, budget, customer_level as customerLevel, customer_source as customerSource," +
            "ut.nickname from area_customer as ac inner join user_t as ut on ac.u_id = ut.id order by create_time DESC")
    List<CustomerInfoVo> getCustomerInfoListAll();

    @Select("select ac.id,customer_name as customerName,customer_number as customerNumber, customer_area as customerArea," +
            "customer_wechat as customerWechat,customer_contact as customerContact,create_time as createTime, budget, customer_level as customerLevel, customer_source as customerSource," +
            "ut.nickname from area_customer as ac inner join user_t as ut on ac.u_id = ut.id where u_id = #{uid} order by create_time DESC")
    List<CustomerInfoVo> getCustomerInfoListPerson(int uid);


    @Insert("insert into area_customer values(null,#{customerName}, #{customerNumber}, #{customerArea}, #{budget}, #{customerWechat}" +
            ",#{customerLevel}, #{customerSource}, #{createTime},#{customerContact}, #{uId})")
    void addCustomer(CustomerInfo info);

    //@SelectProvider(type = CustomerProvider.class, method = "findCustomerByNameAndPhone")
    @Select({"<script>",
            "select ac.id,customer_name as customerName,customer_number as customerNumber,customer_area as customerArea," +
                    "customer_wechat as customerWechat,customer_contact as customerContact,create_time as createTime, budget, " +
                    "customer_level as customerLevel, customer_source as customerSource,ut.nickname from area_customer as ac " +
                    "inner join user_t as ut on ac.u_id = ut.id where 1=1",
            "<when test='customerNumber !=null'>",
            "AND customer_number like #{customerNumber}",
            "</when>",
            "<when test='customerName !=null'>",
            "AND customer_name like #{customerName}",
            "</when>",
            "<when test='ids !=null'>",
            "AND u_id in"
                    + "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>"
                    + "#{item}"
                    + "</foreach>",
            "</when>",
            " order by create_time DESC",
            "</script>"})
    List<CustomerInfoVo> search(@Param("customerNumber") String customerNumber, @Param("customerName") String customerName,
                                @Param("ids") int[] ids);

    @Select("select * from area_schedule where a_id = #{aId}")
    List<CustomerContactMessage> getMessageById(int aId);

    @Insert("insert into area_schedule values(null,#{content},#{contactTime},#{aId})")
    void insertSchedule(CustomerContactMessage message);

    @Delete("delete from area_customer where id =#{id}")
    void deleteCustomerInfo(int id);

    @Delete("delete from area_schedule where a_id = #{id}")
    void deleteInfoMessage(int id);

    @Update("update area_customer set customer_name = #{customerName},customer_number=#{customerNumber},customer_area=#{customerArea}," +
            "customer_wechat=#{customerWechat},customer_contact=#{customerContact},budget=#{budget},customer_level=#{customerLevel}," +
            "customer_source=#{customerSource} where id = #{id}")
    void updateInfoById(CustomerInfo info);
}
